﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace CalculatorGUI
{
    public partial class CalcControl : UserControl
    {
        // Constructor
        public CalcControl()
        {
            InitializeComponent();
            textBox1.Text = "";
        }

        CalculatorLogic.EquationParser parser = new CalculatorLogic.EquationParser();
        CalculatorLogic.PostfixSolver solver = new CalculatorLogic.PostfixSolver();
        CalculatorLogic.Algebraic_Math.Solve algebraSolver = new CalculatorLogic.Algebraic_Math.Solve();
        CalculatorLogic.Algebraic_Math.Factor factorSolver = new CalculatorLogic.Algebraic_Math.Factor();
        CalculatorLogic.Algebraic_Math.Expand expandSolver = new CalculatorLogic.Algebraic_Math.Expand();
        CalculatorLogic.Algebraic_Math.Zeros zerosSolver = new CalculatorLogic.Algebraic_Math.Zeros();

        CalculatorLogic.Equation algebraEquation = new CalculatorLogic.Equation();
        CalculatorLogic.Equation factorEquation = new CalculatorLogic.Equation();
        CalculatorLogic.Equation expandEquation = new CalculatorLogic.Equation();
        CalculatorLogic.Equation zerosEquation = new CalculatorLogic.Equation();

        //NUMBERS
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "1";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "3";
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "4";
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "5";
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "6";
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "7";
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "8";
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "9";
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "0";
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += ".";
        }

        //NEGATIVE SYMBOL
        private void button15_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " -";
        }

        //BASIC MATH
        private void button8_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " + ";
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " - ";
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " * ";
        }

        private void button20_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " / ";
        }

        //PARENTHESES
        private void button28_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " ( ";
        }

        private void button29_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " ) ";
        }

        //EXPONENT
        private void button22_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " ^ ";
        }

        //TRIG
        private void button17_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " SIN ( ";
        }

        private void button18_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " COS ( ";
        }

        private void button19_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " TAN ( ";
        }

        private void button33_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " CSC ( ";
        }

        private void button34_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " COT ( ";
        }

        private void button35_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " SEC ( ";
        }

        //LOGARITHMIC FUNCTIONS
        private void button21_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " LOG ( ";
        }

        private void button27_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " LN ( ";
        }
        //EQUALS (ALGEBRAIC CALCULATIONS)
        private void button23_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " = ";
        }
        //X
        private void button30_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "x";
        }
        //PI
        private void button36_Click_1(object sender, RoutedEventArgs e)
        {
            textBox1.Text += Math.PI;
        }

        //e
        private void button37_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += Math.E;
        }

        //CLEAR
        private void button32_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = string.Empty;
        }

        //BACKSPACE
        private void button31_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
            }
        }


        //FACTORING
        private void button24_Click(object sender, RoutedEventArgs e)
        {
            factorEquation.Input[0] = GetLastLine(textBox1.Text).Replace(" ", "");
            factorSolver.Apply(factorEquation);
            textBox1.Text += " = \r";
            textBox1.Text += factorEquation.Solved;
            textBox1.Text += "\r";

            autoScroll();
        }

        //EXPAND
        private void button25_Click(object sender, RoutedEventArgs e)
        {
            string expand = GetLastLine(textBox1.Text);
            expand = expand.Replace(" ", "");
            expand = expand.Replace("+", " + ");
            expandEquation.Input[0] = expand.Trim();
            expandSolver.Apply(expandEquation);
            textBox1.Text += " = \r";
            textBox1.Text += expandEquation.Solved;
            textBox1.Text += "\r";

            autoScroll();
        }

        //ZEROS
        private void button26_Click(object sender, RoutedEventArgs e)
        {
            zerosEquation.Input[0] = GetLastLine(textBox1.Text).Replace(" ", "");
            zerosSolver.Apply(zerosEquation);
            textBox1.Text += " = \r";
            textBox1.Text += zerosEquation.Solved;
            textBox1.Text += "\r";

            autoScroll();
        }

        //ENTER
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            if (GetLastLine(textBox1.Text).Contains("="))
            {
                algebraEquation.Input[0] = GetLastLine(textBox1.Text).Replace(" ", "");
                algebraSolver.Apply(algebraEquation);
                textBox1.Text += " = \r";
                textBox1.Text += algebraEquation.Solved;
                textBox1.Text += "\r";
            }

            else
            {
                string answer = Solve(GetLastLine(textBox1.Text));
                textBox1.Text += " = \r";
                textBox1.Text += answer.PadLeft(65 - (answer.Length));
                textBox1.Text += "\r";
            }
            autoScroll();
        }

        public string Solve(string input)
        {
            string temp = parser.Parse(input);
            string answer = solver.Solve(temp);
            return answer;
        }

        public string GetLastLine(string text)
        {
            string[] lines = Regex.Split(textBox1.Text, "\r");
            return lines.Last();
        }

        /// <summary>
        /// Scrolls the textbox automatically as the height increases
        /// </summary>
        private void autoScroll()
        {
            if (textBox1.ActualHeight > scrollViewer1.ViewportHeight)
            {
                scrollViewer1.ScrollToVerticalOffset(scrollViewer1.ScrollableHeight + textBox1.ActualHeight);
            }
        }

        //private void graphingGUI()
        //{
        //    if (graphing)
        //    {
        //        scrollViewer1.Visibility = Visibility.Collapsed;
        //        graphControl1.Visibility = Visibility.Visible;
        //    }
        //    else
        //    {
        //        scrollViewer1.Visibility = Visibility.Visible;
        //        graphControl1.Visibility = Visibility.Collapsed;
        //    }
        //}
    }
}
